package com.jandan.ui.client;
import java.util.List;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.form.ComboBox;
import com.jandan.ui.client.service.WordLibService;
import com.jandan.ui.client.service.WordLibServiceAsync;
import com.jandan.ui.model.WordLib;

public class SmallLibCountComboBox extends ComboBox {
	public SmallLibCountComboBox() {
		this.setForceSelection(true);
		this.setMinChars(1);
		this.setFieldLabel("wordsCountOfPerSmallLib");
		this.setDisplayField("wordsCountOfPerSmallLib");
		this.setValueField("wordsCountOfPerSmallLib");
		this.setMode(ComboBox.LOCAL);
		this.setTriggerAction(ComboBox.ALL);
		this.setTypeAhead(true);
		this.setSelectOnFocus(true);
//		this.setEmptyText("请选择词库");
		this.setWidth(150);
		this.setEditable(false);
		
		this.setHideTrigger(false);
		
		WordLibServiceAsync wordLibService = WordLibService.Util.getInstance();
		wordLibService.getWordLibList(new AsyncCallback<List<WordLib>>(){
			public void onFailure(Throwable caught) {
				MessageBox.alert(caught.getMessage());
			}

			public void onSuccess(List<WordLib> result) {
				// TODO Auto-generated method stub
				final Store store = new SimpleStore(new String[] { "wordsCountOfPerSmallLib" }, getWordLibs(result));
				store.load();
				setSotre(store);
			}

		});
	}
	
	private void setSotre(Store store){
		this.setStore(store);
	}

	private String[][] getWordLibs(List<WordLib> wordLibList) {		
		String[][] wordLibs = new String[wordLibList.size()][];
		int size =  wordLibList.size();
		for(int i = 0;i < size;i++){
			wordLibs[i] = new String[2];
			wordLibs[i][0] = "" + wordLibList.get(i).getWordLibID();
			wordLibs[i][1] = wordLibList.get(i).getWordLibName();
		}
		return wordLibs;
	}
	
	
}
